package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.Result;
import com.heima.mapper.SkuClassMapper;
import com.heima.domain.dto.SkuClassDto;
import com.heima.domain.po.SkuClass;
import com.heima.domain.vo.SkuClassVo;
import com.heima.service.SkuClassService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class SkuClassServiceImpl extends ServiceImpl<SkuClassMapper,SkuClass> implements SkuClassService {

    @Autowired
    private SkuClassMapper skuClassMapper;

    /**
     * 新增分类
     *
     * @param dto
     */
    @Override
    public Boolean addSort(SkuClassDto dto) {

        try {
            skuClassMapper.addSort(dto.getClassName());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return null;
    }

    /**
     * 分页查询商品分类
     *
     * @param pageIndex
     * @param pageSize
     * @param className
     * @return
     */
    @Override
    public Result getByPage(Integer pageIndex, Integer pageSize, String className) {

        Page<SkuClass> page = new Page<>(pageIndex, pageSize);
        QueryWrapper<SkuClass> wrapper = new QueryWrapper<SkuClass>();
        if(StringUtils.hasText(className)){
            wrapper.lambda().eq(SkuClass::getClassName, className);
        }
        Page<SkuClass> page1 = this.page(page, wrapper);
        Result of = Result.of(page1);
        return of;
    }

    /**
     * 修改商品分类
     *
     * @param classId
     * @param dto
     * @return
     */
    @Override
    public Boolean updateSku(String classId, SkuClassDto dto) {
        UpdateWrapper<SkuClass> wrapper = new UpdateWrapper<>();
        wrapper.lambda().set(SkuClass::getClassName, dto.getClassName()).eq(SkuClass::getClassId, Integer.parseInt(classId));
        boolean update = this.update(wrapper);
        return update;
    }

    /**
     * 获取商品分类
     *
     * @param classId
     * @return
     */
    @Override
    public SkuClassVo getSkuClassById(Integer classId) {
        SkuClass skuClass = skuClassMapper.selectById(classId);
        SkuClassVo skuClassVo = new SkuClassVo();
        BeanUtils.copyProperties(skuClass, skuClassVo);
        return skuClassVo;
    }

    /**
     * 删除商品分类
     *
     * @param classId
     */
    @Override
    public void deleteSkuClassById(Integer classId) {
        Integer count = skuClassMapper.countById(classId);
        if(count > 0){
            throw new RuntimeException("商品分类下有商品，不能删除");
        }
        QueryWrapper<SkuClass> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(SkuClass::getClassId,classId);
        skuClassMapper.delete(wrapper);
    }


}